Skip to content

Experimental: Add opt-in v2 parser that returns the existing AST - #2996

Draft
andygrove wants to merge 2 commits into
apache:mainfrom
andygrove:v2-bridge-prototype
Draft

andygrove wants to merge 2 commits into
apache:mainfrom
andygrove:v2-bridge-prototype

Conversation

@andygrove

@andygrove andygrove commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Dialects: none

Experimental, not ready to merge. I am experimenting with how the v2 parser proposed in #2893 could be added incrementally, without breaking changes to the existing AST. I am opening this to get feedback on the approach.

This adds the v2 lexer, parser and AST as a separate crate, sqlparser_v2. Its serde, visitor and comment support are left out, since the conversion does not need them.

It also adds an opt-in v2 feature to sqlparser. sqlparser::v2::parse_sql parses with v2 and converts the result to the existing AST, so callers can try the new parser without changing their code. Nothing changes for users who do not enable the feature. GenericDialect and AnsiDialect have no v2 equivalent and are rejected. Only queries are converted so far. Anything else fails with Error::Unconverted, and parse_sql_with_fallback retries those with the existing parser. compare runs both parsers, for checking a downstream test suite against v2.

tests/sqlparser_v2.rs runs every statement in v2's corpus through both parsers in each dialect. 14,640 conversions produce the same AST as the existing parser. 307 differ because the two parsers read the SQL differently, and are listed in tests/v2_known_differences.txt. The test fails on a new difference, or if the number of matching conversions drops.

With PostgreSQL, v2 plus the conversion is faster than the existing parser (sqlparser_bench/benches/v2_bridge.rs):

Query Existing parser v2 + conversion Speedup
SELECT * FROM my_table WHERE 1 = 1 2.50 µs 1.78 µs 1.4x
WITH query with a join 9.66 µs 5.29 µs 1.8x
TPC-H Q5 22.2 µs 11.0 µs 2.0x
100 KB statement 3.87 ms 1.68 ms 2.3x

The code in this PR was written by Claude (Claude Code). The design is mine, and I understand it well, but I have not reviewed every line of the code.

@codecov-commenter

codecov-commenter commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.23%. Comparing base (9429cd3) to head (9e7b50c).
⚠️ Report is 54 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2996      +/-   ##
==========================================
+ Coverage   83.29%   84.23%   +0.93%     
==========================================
  Files          42       44       +2     
  Lines       35378    36193     +815     
  Branches    35378    36193     +815     
==========================================
+ Hits        29469    30486    +1017     
+ Misses       2456     2341     -115     
+ Partials     3453     3366      -87     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

andygrove and others added 2 commits November 18, 2026 09:02
Adds the lexer, parser and AST of the v2 parser from the 2.0 proposal
(apache#2893) as a separate crate, sqlparser_v2, without its serde, visitor and
comment support. sqlparser does not depend on it yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sqlparser::v2::parse_sql parses with the v2 parser and converts its AST to
this crate's AST, for the built-in dialects that v2 has. GenericDialect and
AnsiDialect have no v2 equivalent and are rejected. Nodes with no
conversion yet fail with Error::Unconverted, and parse_sql_with_fallback
retries those with this crate's parser. compare runs both parsers.

tests/sqlparser_v2.rs checks every statement in tests/v2_corpus.txt against
this crate's parser in each dialect: a converted AST must equal this
crate's, apart from the cases in v2_known_differences.txt, where the two
parsers read the SQL differently.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants